<?php
/**
 *
 * @author dinglin
 * 每日打卡最终状态表 user_day_status
 */
class Oa_Data_UserDayStatus{

    private static $instance;
    public static function get_instance(){
        if(!self::$instance) {
            self::$instance = new Oa_Data_UserDayStatus();
        }
        return self::$instance;
    }
    //所有可能用户状态基准数据
    private static function user_day_status(){
        $fields = array('user_id','dateline','days','am_status','am_type','am_leave_status','pm_status','pm_type','pm_leave_status','is_later','leave_early');
        $values =
        array(
                array(0,0,0.0,5,0,0,6,0,0,0,0),//正常
                array(0,0,0.5,3,0,0,6,0,0,0,0),//上午缺勤
                array(0,0,0.0,1,14,4,6,0,0,1,0),//迟到

                array(0,0,0.5,5,0,0,4,0,0,0,0),//下午缺勤
                array(0,0,1,3,0,0,4,0,0,0,0),//全天缺勤
                array(0,0,0.5,1,0,0,4,0,0,1,0),//迟到，下午缺勤

                array(0,0,0.0,5,0,0,2,0,0,0,1),//早退
                array(0,0,0.5,3,0,0,2,0,0,0,1),//上午缺勤，早退
                array(0,0,0.0,1,0,0,2,0,0,1,1),//迟到，早退

        );
        $leave_type=array(4,5,6,7,8,9,10,11,12,13);//不包括迟到，早退，缺勤，公出
        //数据组合
        $result = array();
        foreach($values as $key=> $value){
            foreach ($fields as $k => $f){
                $result[$key][$f] = $value[$k];
            }
        }
        //所有可能请假，打卡状态，假期批准状态组合
        $i = count($result);
        foreach($result as $key => $value){
            foreach($leave_type as $va){
                for($j=0;$j<3;$j++){
                    $result[$i] = $value;
                    if($value['am_status'] == 5 && (0==$j || 1==$j)){
                        $result[$i]['am_type']=$va;
                        $result[$i]['am_leave_status']=2;
                    }elseif(0==$j || 1==$j){
                        $result[$i]['am_type']=$va;
                        $result[$i]['am_leave_status']=1;
                    }
                    if($value['pm_status'] == 5 &&(0==$j || 2==$j)){
                        $result[$i]['pm_type']=$va;
                        $result[$i]['pm_leave_status']=2;
                    }elseif(0==$j || 2==$j){
                        $result[$i]['pm_type']=$va;
                        $result[$i]['pm_leave_status']=1;
                    }
                    $i++;
                }
            }
        }
        return $result;
    }
    /**
     * 一个用户的不同日期考勤状态
     * 不能跨月
     * $dateline = Ymd
     * $start = int
     */
   public static function get_user_day_status($user_id,$dateline,$start,$end,$is_one_date = true){
       $user_day_status =  self::user_day_status();
       $result = array();
       foreach($user_day_status as $index=>$user){
           if($index >= $start && $index< $end){
               $result[$index] = $user;
               if($is_one_date){
                   $result[$index]['dateline'] = $dateline;
               }else{
                   $result[$index]['dateline'] = $dateline;
                   $dateline++;
               }
               $result[$index]['user_id'] = $user_id;
           }
       }
       unset($user_day_status);
       return $result;
   }
   /**
    * 多个用户的某天考勤状态$user_ids=array(5653,6547)
    * $dateline = Ymd
    * $start = int
    */
   public static function get_users_day_status($user_ids,$dateline,$start,$end,$is_one_user = true){
       $user_day_status =  self::user_day_status();
       $result = array();
       $i=0;
       foreach($user_day_status as $index=>$user){
           if($index >= $start && $index< $end){
               $result[$index] = $user;
               if($is_one_user){
                   $result[$index]['user_id'] = $user_ids;
               }else{
                   $result[$index]['user_id'] = $user_ids[$i];
                   $i++;
               }
               $result[$index]['dateline'] = $dateline;
           }
       }
       return $result;
   }
}